<?php

namespace app\api\controller;

use addons\wechat\library\Config as ConfigService;
use app\api\model\Order;
use app\api\model\OrderGoods;
use app\api\model\ShopCategory;
use app\api\model\ShopGoodsSpec;
use app\api\model\UserAddress;
use app\api\model\UserSearchLog;
use app\common\controller\Api;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order as WxOrder;
use fast\Tree;
use think\Cache;
use think\Db;
use think\Model;
use think\Validate;

/**
 * G-商品接口
 * @ApiWeigh    (95)
 */
class Goods extends Api {
	protected $noNeedLogin = ['*'];

	public function _initialize() {
		parent::_initialize();
	}

//	/**
//	 * @ApiTitle    (一级分类)
//	 * @ApiSummary  (一级分类)
//	 * @ApiMethod (POST)
//	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
//	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
//	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
//	 * @ApiReturn   ({
//	"code": 1,
//	"msg": "一级分类",
//	"data": [
//	{
//	"id": 6,
//	"name": "分类名称",
//	"image": "分类图片"
//	}
//	]
//	})
//	 */
//	public function getCategoryList() {
//		$cateModel = new ShopCategory();
//		$category  = collection( $cateModel->field( 'id,name,image' )->where( ['pid' => 0] )->order( 'weigh', 'desc' )->select() )->toArray();
//		$this->success( '一级分类', $category );
//	}
//
//	/**
//	 * @ApiTitle    (分类商品新)
//	 * @ApiSummary  (根据分类获取商品商品)
//	 * @ApiMethod (POST)
//	 * @ApiParams   (name=pid, type=integer, required=true, description="一级分类ID")
//	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
//	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
//	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
//	 * @ApiReturn   ({
//	"code": 1,
//	"msg": "分类商品",
//	"time": "1579416085",
//	"data": [
//	{
//	"id": 9,
//	"pid": 6,
//	"name": "二级分类名称",
//	"spacer": "&nbsp;├",
//	"childlist": [
//	{
//	"id": "商品ID",
//	"name": "商品名称",
//	"describe": "简介",
//	"goods_no": "商品货号",
//	"image": "缩略图",
//	"spec_type": "20",
//	"goods_sales": "销量",
//	"goods_min_price": "最低金额",
//	"goods_max_price": "最高金额",
//	"spec": [
//	{
//	"goods_spec_id": "规格ID",
//	"goods_id": "商品ID",
//	"goods_no": "货号",
//	"goods_price": "售价",
//	"line_price": "划线价",
//	"stock_num": "库存数量",
//	"goods_sales": "销量",
//	"spec_sku_id": "sku组合",
//	"spec_image": "规格缩略图",
//	"score": "返积分"
//	}
//	],
//	"active": [
//	{
//	"id": "活动ID",
//	"name": "活动名称"
//	}
//	]
//	}
//	]
//	}
//	]
//	})
//	 */
//	public function getCategoryGoods() {
//		$id       = $this->request->param( 'pid', 0, 'intval' );
//		$category = Cache::get( 'category_goods' . $id );
//		if ( empty( $category ) ) {
//			$cateModel = new ShopCategory();
//			$category  = collection( $cateModel->field( 'id,name,image' )->where( ['pid' => $id] )->order( 'weigh', 'desc' )->select() )->toArray();
//			$model     = new \app\api\model\Goods();
//			foreach ( $category as $key => &$val ) {
//				$child            = $model->getList( ['category_id' => $val['id']] );
//				$val['childlist'] = $child;
//			}
//			Cache::set( 'category_goods' . $id, $category );
//		}
//		$this->success( '分类商品', $category );
//	}
//
//	/**
//	 * @ApiTitle    (分类商品)
//	 * @ApiSummary  (分类商品)
//	 * @ApiMethod (POST)
//	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
//	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
//	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
//	 * @ApiReturn   ({
//	"code": 1,
//	"msg": "分类商品",
//	"time": "1579416085",
//	"data": [
//	{
//	"id": 6,
//	"pid": 0,
//	"name": "一级分类名称",
//	"spacer": "",
//	"childlist": [
//	{
//	"id": 9,
//	"pid": 6,
//	"name": "二级分类名称",
//	"spacer": "&nbsp;├",
//	"childlist": [
//	{
//	"id": "商品ID",
//	"name": "商品名称",
//	"describe": "简介",
//	"goods_no": "商品货号",
//	"image": "缩略图",
//	"spec_type": "20",
//	"goods_sales": "销量",
//	"goods_min_price": "最低金额",
//	"goods_max_price": "最高金额",
//	"spec": [
//	{
//	"goods_spec_id": "规格ID",
//	"goods_id": "商品ID",
//	"goods_no": "货号",
//	"goods_price": "售价",
//	"line_price": "划线价",
//	"stock_num": "库存数量",
//	"goods_sales": "销量",
//	"spec_sku_id": "sku组合",
//	"spec_image": "规格缩略图",
//	"score": "返积分"
//	}
//	],
//	"active": [
//	{
//	"id": "活动ID",
//	"name": "活动名称"
//	}
//	]
//	}
//	]
//	}
//	]
//	}
//	]
//	})
//	 */
//	public function category() {
//		$list = Cache::get( 'category_goods' );
//		if ( empty( $list ) ) {
//			$cateModel = new ShopCategory();
//			$category  = collection( $cateModel->field( 'id,pid,name,image' )->order( 'weigh', 'desc' )->select() )->toArray();
//			$tree      = Tree::instance();
//			$tree->init( $category );
//			$list  = $tree->getTreeArray( 0 );
//			$model = new \app\api\model\Goods();
//			foreach ( $list as $k => &$v ) {
//				if ( !empty( $v['childlist'] ) ) {
//					foreach ( $v['childlist'] as $key => &$val ) {
//						$child            = $model->getList( ['category_id' => $val['id']] );
//						$val['childlist'] = $child;
//					}
//				}
//			}
//			Cache::set( 'category_goods', $list );
//		}
//
//		$this->success( '分类商品', $list );
//	}
//
//	/**
//	 * @ApiTitle    (商品列表)
//	 * @ApiSummary  (商品列表)
//	 * @ApiMethod (POST)
//	 * @ApiParams   (name=page, type=string, required=true, description="页码")
//	 * @ApiParams   (name=name, type=integer, required=false, description="商品名称")
//	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
//	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
//	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
//	 * @ApiReturn   ({
//	"code":"状态码",
//	"msg": "提示消息",
//	"data": [
//	{
//	"id": "商品ID",
//	"name": "商品名称",
//	"describe": "简介",
//	"goods_no": "商品货号",
//	"image": "缩略图",
//	"spec_type": "20",
//	"goods_sales": "销量",
//	"goods_min_price": "最低金额",
//	"goods_max_price": "最高金额",
//	"spec": [
//	{
//	"goods_spec_id": "规格ID",
//	"goods_id": "商品ID",
//	"goods_no": "货号",
//	"goods_price": "售价",
//	"line_price": "划线价",
//	"stock_num": "库存数量",
//	"goods_sales": "销量",
//	"spec_sku_id": "sku组合",
//	"spec_image": "规格缩略图",
//	"score": "返积分"
//	}
//	],
//	"active": [
//	{
//	"id": "活动ID",
//	"name": "活动名称"
//	}
//	]
//	}
//	]
//	})
//	 */
//	public function index() {
//		$param = $this->request->param();
//		if ( !empty( $param['name'] ) && $this->auth->isLogin() ) {
//			$model  = new UserSearchLog();
//			$userId = $this->auth->id;
//			$row    = $model->where( ['user_id' => $userId, 'keyword' => $param['name']] )->find();
//			if ( $row ) {
//				$row->save( ['updatetime' => time()] );
//			} else {
//				$model->save( ['user_id' => $userId, 'keyword' => $param['name']] );
//			}
//		}
//		//商品表名称
//		$model = new \app\api\model\Goods();
//		$data  = $model->getList( $param, true );
//		$this->success( '', $data );
//	}
//
//	/**
//	 * @ApiTitle    (商品详情)
//	 * @ApiSummary  (商品商品详情列表)
//	 * @ApiMethod (POST)
//	 * @ApiParams   (name=id, type=string, required=true, description="商品ID")
//	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
//	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
//	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
//	 * @ApiReturn   ({
//	"code":"状态码",
//	"msg":"提示消息",
//	"data":{
//	"detail":{
//	"id":"商品ID",
//	"name": "商品名称",
//	"describe": "简介",
//	"goods_no":"货号",
//	"category_id":"分类ID",
//	"brand_id":"品牌ID",
//	"image":"缩略图",
//	"images":[
//	"轮播图"
//	],
//	"spec_type":"商品规格:10=单规格,20=多规格",
//	"content":"图文内容",
//	"is_collection":"是否收藏:0=否,1=是",
//	"goods_sales":"商品销量",
//	"spec":[
//	{
//	"goods_spec_id":"商品规格ID",
//	"goods_id":"商品ID",
//	"goods_no":"商品货号",
//	"goods_price":"商品售价",
//	"line_price":"划线价格",
//	"stock_num":"库存数量",
//	"goods_sales":"销量",
//	"goods_weight":"商品重量",
//	"spec_sku_id":"SKU_ID",
//	"spec_image":"规格图片",
//	"score":"赠送积分"
//	}
//	],
//	"spec_rel":[
//
//	],
//	"active":[
//	{
//	"id":"活动ID",
//	"name":"活动名称"
//	}
//	]
//	},
//	"specData":{
//	"spec_attr":[
//	{
//	"group_id":"规格组ID",
//	"group_name":"规格名称",
//	"spec_items":[
//	{
//	"item_id":"规格ID",
//	"spec_value":"规格值"
//	}
//	],
//	"spec_list":[
//	{
//	"goods_spec_id":125,
//	"spec_sku_id":"sku_id",
//	"rows":[
//
//	],
//	"form":{
//	"goods_no":"商品货号",
//	"goods_price":"商品售价",
//	"line_price":"划线价格",
//	"stock_num":"库存数量",
//	"goods_sales":"销量",
//	"goods_weight":"商品重量",
//	"spec_sku_id":"SKU_ID",
//	"spec_image":"规格图片",
//	"score":"赠送积分"
//	}
//	}
//	]
//	}
//	]
//	}
//	}
//	})
//	 */
//	public function detail() {
//		$id = $this->request->request( 'id' );
//		// 商品详情
//		$detail = \app\api\model\Goods::detail( $id );
//		if ( !$detail || $detail['status'] !== 1 ) {
//			$this->error( '很抱歉，商品信息不存在或已下架' );
//		}
//		// 规格信息
//		$specData = $detail['spec_type'] === '20' ? $detail->getManySpecData( $detail['spec_rel'], $detail['spec'] ) : null;
//
//		$userId                  = $this->auth->id;
//		$isCollection            = Db::name( 'user_collection' )->where( ['user_id' => $userId, 'goods_id' => $id] )->find();
//		$detail['is_collection'] = empty( $isCollection ) ? 0 : 1;
//		$model                   = new \app\api\model\Cart();
//		$cartNum                 = $model->getTotalNum( $userId );
//		$this->success( '', ['detail' => $detail, 'specData' => $specData, 'cartNum' => $cartNum] );
//	}

	/**
	 * 直接购买数据
	 *
	 * @ApiTitle    (直接购买数据)
	 * @ApiSummary  (点击商品详情页的立即购买按钮显示购买商品详情页数据)
	 * @ApiMethod   (POST)
	 * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
	 *
	 * @ApiParams   (name="goods_id", type="integer", required=true, description="商品ID")
	 * @ApiParams   (name="num", type="integer", required=true, description="数量")
	 *
	 * @ApiReturnParams   (name="goods_list[]", type="array", required=true, description="商品信息[]")
	 * @ApiReturnParams   (name="id", type="array", required=true, description="商品id")
	 * @ApiReturnParams   (name="name", type="string", required=true, description="商品名称")
	 * @ApiReturnParams   (name="goods_no", type="string", required=true, description="商品编号")
	 * @ApiReturnParams   (name="category_id", type="string", required=true, description="商品类别:1=画作,2=指挥手办,3=摄影技术,4=最新作品")
	 * @ApiReturnParams   (name="image", type="string", required=true, description="商品一张缩略图")
	 * @ApiReturnParams   (name="images", type="string", required=true, description="商品图组")
	 * @ApiReturnParams   (name="goods_price", type="float", required=true, description="商品单价")
	 * @ApiReturnParams   (name="stock_num", type="integer", required=true, description="商品库存")
	 * @ApiReturnParams   (name="total_num", type="integer", required=true, description="购买数量")
	 * @ApiReturnParams   (name="goods_list['total_price']", type="float", required=true, description="合计价格:仅该商品*购物车数量价格,该项目不展示此字段,支付总价格用goods_list[]同层级的total_price字段")
	 * @ApiReturnParams   (name="total_price", type="float", required=true, description="总支付金额")
	 *
	 * @ApiReturnParams   (name="address_info", type="object", required=true, description="默认地址信息")
	 * @ApiReturnParams   (name="address_info.id", type="integer", required=true, description="默认地址信息id")
	 * @ApiReturnParams   (name="address_info.name", type="string", required=true, description="地址收件人")
	 * @ApiReturnParams   (name="address_info.mobile", type="string", required=true, description="手机号码****")
	 * @ApiReturnParams   (name="address_info.address", type="string", required=true, description="详细地址")
	 * @ApiReturnParams   (name="address_info.address_now", type="string", required=true, description="所在地区")
	 *
	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
	 * @ApiReturn   ({
	"code":"状态码",
	"msg": "提示消息"
	})
	 */
	public function buyData() {
		//* @ApiParams   (name="goods_spec_id", type="integer", required=true, description="规格ID")
		$rules    = [
			//'goods_spec_id' => 'require',
			'goods_id' => 'require',
			'num'      => 'require|integer|gt:0',
		];
		$message  = [
			//'goods_spec_id.require' => '规格ID不能为空',
			'goods_id.require' => '商品ID不能为空',
			'num.require'      => '商品数量不能为空',
		];
		$validate = new Validate( $rules, $message );
		$param    = $this->request->param();
		if ( !$validate->check( $param ) ) {
			$this->error( $validate->getError() );
		}
		//获取购买商品数据
		$cartData = [
			[
				//'goods_spec_id' => $param['goods_spec_id'],
				'cart_id'  => 0, //直接购买 不传购物车id
				'goods_id' => $param['goods_id'],
				'num'      => $param['num']
			]
		];
		$model    = new \app\api\model\Cart();
		//商品订单详情
		$list = $model->getListByIds( 0, $this->auth->id, $cartData );
		if ( $list['has_error'] ) {
			$this->error( $list['error_msg'] );
		}
		//从商品详情购买单个商品时的下单数据可这么处理,要和购物车下单数据区分,购物车要循环计算返回
		$list['total_price'] = $list['goods_list'][0]['total_price'];

		//获取默认地址信息
		$userAddressModel     = new UserAddress();
		$field                = 'id,name,mobile,address,address_now';
		$where                = ['user_id' => $this->auth->id, 'is_default' => 1];
		$default_address_info = $userAddressModel->field( $field )->where( $where )->find();
		//手机号*****
		$default_address_info['mobile'] = substr( $default_address_info['mobile'], 0, 3 ) . '****' . substr( $default_address_info['mobile'], 7 );
		$list['address_info']           = $default_address_info;

		$this->success( '购物车结算数据', $list );
	}

	/**
	 * 直接购买支付
	 *
	 * @ApiTitle    (直接购买支付)
	 * @ApiSummary  (直接购买支付)
	 * @ApiMethod   (POST)
	 * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
	 *
	 * @ApiParams   (name="goods_id", type="integer", required=true, description="商品ID")
	 * @ApiParams   (name="num", type="integer", required=true, description="数量")
	 * @ApiParams   (name="address_id", type="integer", required=true, description="地址ID")
	 * @ApiParams   (name="open_id", type="string", required=true, description="用户openId-通过A登录相关接口获取")
	 *
	 * @ApiReturnParams   (name="payDetail", type="string", required=true, description="支付信息")
	 * @ApiReturnParams   (name="order_id", type="integer", required=true, description="订单id")
	 * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
	 * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
	 * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
	 * @ApiReturn   ({
	"code":"状态码",
	"msg": "提示消息"
	})
	 */
//* @ApiParams   (name="goods_spec_id", type="integer", required=true, description="规格ID")
//* @ApiParams   (name="voucher_id", type="integer", required=false, description="购物券ID")
//* @ApiParams   (name="coupon_id", type="integer", required=false, description="优惠券ID")
//* @ApiParams   (name="score", type="integer", required=false, description="积分")
//* @ApiParams   (name="remark", type="string", required=false, description="备注")
	public function pay() {
		$rules    = [
			'goods_id'   => 'require',
//			'goods_spec_id' => 'require',
			'num'        => 'require|integer|gt:0',
			'address_id' => 'require',
			'open_id'    => 'require',
		];
		$message  = [
			'goods_id.require'   => '商品ID不能为空',
//			'goods_spec_id.require' => '规格ID不能为空',
			'num.require'        => '商品数量不能为空',
			'address_id.require' => '请选择收货地址',
			'open_id.require'    => 'openid不能为空',
		];
		$validate = new Validate( $rules, $message );
		$param    = $this->request->param();
		if ( !$validate->check( $param ) ) {
			$this->error( $validate->getError() );
		}
//		$cartData = [['cart_id' => 0, 'goods_id' => $param['goods_id'], 'goods_spec_id' => $param['goods_spec_id'], 'num' => $param['num']]];
		$cartData = [['cart_id' => 0, 'goods_id' => $param['goods_id'], 'num' => $param['num']]];
		$address  = Db::name( 'user_address' )->where( ['id' => $param['address_id']] )->find();
		if ( empty( $address ) ) {
			$this->error( '地址不存在' );
		}
		$model = new \app\api\model\Cart();
		$list  = $model->getListByIds( 0, $this->auth->id, $cartData );
		if ( $list['has_error'] ) {
			$this->error( $list['error_msg'] );
		}
//		$address = Db::name( 'user_address' )->where( ['id' => $param['address_id']] )->find();
//		if ( empty( $address ) ) {
//			$this->error( '地址不存在' );
//		}
		$userId = $this->auth->id;
//		if ( !empty( $param['score'] ) ) {
//			$userScore = Db::name( 'user' )->where( ['id' => $userId] )->value( 'score' );
//			if ( $param['score'] > $userScore ) {
//				$this->error( '积分不足' );
//			}
//		}
//		if ( !empty( $param['coupon_id'] ) ) {
//			$couponIds = array_column( $list['coupon_list'], 'id' );
//			if ( in_array( $param['coupon_id'], $couponIds ) ) {
//				$coupon = Db::name( 'user_coupon' )->where( ['id' => $param['coupon_id']] )->find();
//			}
//		} elseif ( !empty( $param['voucher_id'] ) ) {
//			$voucherIds = array_column( $list['voucher_list'], 'id' );
//			if ( in_array( $param['voucher_id'], $voucherIds ) ) {
//				$voucher = Db::name( 'user_voucher' )->where( ['id' => $param['voucher_id']] )->find();
//			}
//		}

		Db::startTrans();
		$orderModel = new Order();
		$orderNo    = get_order_sn( 'D' );
//		$couponPrice  = empty( $coupon ) ? 0 : $coupon['price'];
//		$voucherPrice = empty( $voucher ) ? 0 : $voucher['price'];
//		$scorePrice   = empty( $param['score'] ) ? 0 : bcmul( $param['score'], $list['score_change_price'], 2 );
//		$payPrice     = $list['amount_price'] - $couponPrice - $voucherPrice - $scorePrice + $list['express_price'];
		//查询商品信息
		$goods_information = Db::name('shop_goods')->where('id',$param['goods_id'])->find();
		$payPrice = $goods_information['goods_price'] * $param['num'];
		$status   = 1;
		if ( $payPrice < 0 ) {
			$status   = 2; //订单状态:1=待支付,2=待发货,3=待签收,4=已完成,11=已取消,12=审核驳回,21=售后
			$payPrice = 0;
		}
		$orderData = [
			'order_no'     => $orderNo, //订单编号
			'pay_order_no' => $orderNo,//支付流水号
			'user_id'      => $userId,
			'total_price'  => $goods_information['goods_price'], //商品金额
//			'amount_price'      => $list['amount_price'],
			'pay_price'    => $payPrice, //订单总支付金额
//			'express_price'     => $list['express_price'],
//			'active_save_price' => $list['save_price'],
//			'give_score'        => $list['give_score'],
//			'coupon_price'      => $couponPrice,
//			'voucher_price'     => $voucherPrice,
//			'score_price'       => $scorePrice,
//			'score'        => $param['score'],
//			'remark'       => $param['remark'],
			'sh_name'      => $address['name'],
			'sh_mobile'    => $address['mobile'],
			'sh_address'   => $address['address_now'] . '-' . $address['address'],
			'order_status' => $status,
			'createtime'=>time()
		];
//		$orderModel->save( $orderData );
		$id =$orderModel->insertGetId( $orderData );

		//获取新增订单id
//		$id = $orderModel->id;
//		if ( !empty( $coupon ) ) {
//			Db::name( 'user_coupon' )->where( ['id' => $coupon['id']] )->update( ['status' => 2, 'order_id' => $id] );
//		}
//		$voucherDiffPrice = 0;
//		if ( !empty( $voucher ) ) {
//			$voucherDiffPrice = $voucher['pay_price'];
//			Db::name( 'user_voucher' )->where( ['id' => $voucher['id']] )->update( ['status' => 2, 'order_id' => $id] );
//		}
		$goodsData = [];
		// 更新商品库存 (下单减库存)
		$deductStockData = [];
		$diffPrice       = 0;

		foreach ( $list['goods_list'] as $k => $v ) {
//			$total_diff_price = ( $v['goods_price'] - $v['goods_sku']['cost_price'] ) * $v['total_num']; //毛利润
//			$diffPrice        += $total_diff_price;
			$goodsData[] = [
				'order_id'    => $id,
				'goods_name'  => $v['name'],
				'goods_id'    => $v['id'],
				'goods_no'    => $v['goods_no'],
				'describe'    => $v['describe'],
				'image'       => $v['image'],
//				'deduct_stock_type' => $v['deduct_stock_type'],
//				'spec_type'         => $v['spec_type'],
//				'spec_sku_id'       => $v['goods_sku']['spec_sku_id'],
//				'goods_spec_id'     => $v['goods_sku']['goods_spec_id'],
//				'goods_attr'        => $v['goods_sku']['goods_attr'],
				'goods_price' => $v['goods_price'],
				'total_num'   => $v['total_num'],
				'total_price' => $v['total_price'],
//				'line_price'        => $v['goods_sku']['goods_price'],
//				'cost_price'        => $v['goods_sku']['cost_price'],
//				'diff_price'        => $v['goods_price'] - $v['goods_sku']['cost_price'],
//				'total_diff_price'  => $total_diff_price,
				'user_id'     => $userId,
//				'save_price'        => $v['save_price'],
//				'is_gift'           => $v['is_gift'],
//				'active_id'         => empty( $v['active'][0] ) ? 0 : $v['active'][0]['id'],
//				'active_remark'     => empty( $v['active'][0] ) ? '' : $v['active'][0]['name'],
			];
			// 下单减库存
//			$v['deduct_stock_type'] === '10' && $deductStockData[] = [
//				'goods_spec_id' => $v['goods_sku']['goods_spec_id'],
//				'stock_num' => ['dec', $v['total_num']]
//			];
		}
//		$diffPrice = $orderData['pay_price'] - $list['cost_price'] + $voucherDiffPrice;
//		$orderModel->save( ['diff_price' => $diffPrice] );
		//保存订单商品数据
		$goodsModel = new OrderGoods();
		$goodsModel->saveAll( $goodsData );
		// 下单减库存，更新商品库存
//		!empty( $deductStockData ) && ( new ShopGoodsSpec() )->isUpdate()->saveAll( $deductStockData );

		if ( $status == 2 ) {
			Db::commit();
//			$this->success( '下单成功', ['jsApiParameters' => null, 'id' => $id] );
			$this->success( '下单成功', $id );
		}
//		if ($id){
//			Db::name('shop_goods')->where('id',$param['goods_id'])->setDec('stock_num',$param['num']);
//		}

//		Db::commit(); //测试提交 未支付提交事务 测试添加数据
		//确认订单->唤起微信支付信息->返回支付信息
		$order_info = $orderModel->where( 'id', $id )->find();
		$payservice = new \addons\epay\library\Service();
		//提交预支付订单参数
		$order_param = [
			'amount'    => $order_info['pay_price'],
			'orderid'   => $order_info['order_no'],
			'type'      => 'wechat', //微信
			'title'     => $goods_information['name'],
			'notifyurl' => $this->request->domain() . '/api/notify/service/type/wechat',
			'returnurl' => '',
			'method'    => 'mp', //公众号
			'openid'    => $param['open_id'],
		];
		$result      = $payservice::submitOrder( $order_param );
		if ($result){
			Db::commit();
			$this->success( '支付信息', ['payDetail' => $result, 'order_id' => $order_info['id']] );
		}else{
			Db::rollback();
			$this->error( '支付参数错误' );
		}



		//todo 虚拟金额
		//$orderData['pay_price']=0.01;
//		$openid     = Db::name( 'third' )->where( ['user_id' => $userId] )->value( 'openid' );
//		$attributes = [
//			'trade_type'   => 'JSAPI',
//			'body'         => '订单支付',
//			'detail'       => '订单支付',
//			'out_trade_no' => $orderNo,
//			'total_fee'    => $orderData['pay_price'] * 100, // 单位：分
//			'notify_url'   => url( 'order/notify', [], true, true ),
//			'openid'       => $openid,
//		];
//
//		$order   = new WxOrder( $attributes );
//		$app     = new Application( ConfigService::load() );
//		$payment = $app->payment;
//		$result  = $payment->prepare( $order );
//		if ( $result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS' ) {
//			$prepayId        = $result->prepay_id;
//			$jsApiParameters = $payment->configForJSSDKPayment( $prepayId );
//			Db::commit();
//			$this->success( '预支付', ['jsApiParameters' => $jsApiParameters, 'id' => $id] );
//		} else {
//			Db::rollback();
//			$this->error( '支付参数错误', $result );
//		}
	}

}